﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class main_temp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Session["sTittle"] = "远程教育平台";

        string x = Membership.GetUser().UserName.TrimEnd();
        
        if (x == null) //如果用户名为空，返回登录界面。确保
        { Console.WriteLine("角色错误。");
        Response.Redirect("index.aspx"); 
        }
        string[] y=Roles.GetRolesForUser(x);
        Session["sUserName"] = x;
        Session["sRole"] = y[0];
        //以下读取account中的几个值，生成session变量。
        //strProvider = System.Configuration.ConfigurationManager.AppSettings["gConnectionString"];
        SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["gConnectionString"]);
        string zsql = "select jigou,kecheng,kechenged,voted,username1 from account where username='" + Session["sUserName"] + "'";
        SqlCommand myCommand = new SqlCommand(zsql, MyConnection);
        MyConnection.Open();

        SqlDataReader Reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

       
        //学员：报名参加的课程，已学完的课程，已投票的课程
        //教师：正在进行的课程（终审完成后计入）
        //机构：所有开设的课程（终审完成后计入）
        Reader.Read();
        Session["sJigou"]=Reader.GetValue(0).ToString();
        Session["sKecheng"]=Reader.GetValue(1).ToString();
        Session["sKechenged"]=Reader.GetValue(2).ToString();
        Session["sKecheng_Voted"]=Reader.GetValue(3).ToString();
        Session["sUserName1"] = Reader.GetValue(4).ToString();
        Reader.Close();
        myCommand.Dispose();
        MyConnection.Close();

        //以上读取account中的几个值，生成session变量。
        Conn zconn = new Conn();
        string zdate=DateTime.Now.ToString();
        string zuserip = Request.UserHostAddress.ToString();
        string zsql1 = "insert into details (user_ip,jigou,role,username,username1,xtsj,detail) values ('" + zuserip + "','" + Session["sJigou"].ToString() + "','" + Session["sRole"].ToString() + "','" + Session["sUserName"].ToString() + "','" + Session["sUserName1"].ToString() + "','" + zdate + "','用户登录')";
        int noth = zconn.ExeSql(zsql1);
        zconn.Dispose();
        switch(y[0]){
            case "禁用": Console.WriteLine("您的帐号已被暂时禁用，请与远程教育系统管理员或培训机构管理员联系。"); Response.Redirect("index.aspx"); break;
            default:  Response.Redirect("iframe.aspx"); break;
        }
    }
}
